/***************************************************
 *		版权声明
 *
 *	本操作系统名为：MINE
 *	该操作系统未经授权不得以盈利或非盈利为目的进行开发，
 *	只允许个人学习以及公开交流使用
 *
 *	代码最终所有权及解释权归田宇所有；
 *
 *	本模块作者：	田宇
 *	EMail:		345538255@qq.com
 *
 *
 ***************************************************/

#include "lib.h"
#include "cpu.h"
#include "gate.h"
#include "interrupt.h"
#include "memory.h"
#include "printk.h"
#include "task.h"
#include "trap.h"

struct Global_Memory_Descriptor memory_management_struct = {{0}, 0};

void Start_Kernel(void)
{
    int*         addr = (int*)0xffff800003000000;
    int          i;
    struct Page* page = NULL;
    void*        tmp  = NULL;
    struct Slab* slab = NULL;

    Pos.XResolution = 1440;
    Pos.YResolution = 900;

    Pos.XPosition = 0;
    Pos.YPosition = 0;

    Pos.XCharSize = 8;
    Pos.YCharSize = 16;

    Pos.FB_addr   = (int*)0xffff800003000000;
    Pos.FB_length = (Pos.XResolution * Pos.YResolution * 4 + PAGE_4K_SIZE - 1) & PAGE_4K_MASK;

    load_TR(10);

    set_tss64(_stack_start, _stack_start, _stack_start, 0xffff800000007c00, 0xffff800000007c00,
              0xffff800000007c00, 0xffff800000007c00, 0xffff800000007c00, 0xffff800000007c00,
              0xffff800000007c00);

    sys_vector_init();

    init_cpu();

    memory_management_struct.start_code = (unsigned long)&_text;
    memory_management_struct.end_code   = (unsigned long)&_etext;
    memory_management_struct.end_data   = (unsigned long)&_edata;
    memory_management_struct.end_brk    = (unsigned long)&_end;

    color_printk(RED, BLACK, "memory init \n");
    init_memory();

    color_printk(RED, BLACK, "slab init \n");
    slab_init();

    color_printk(RED, BLACK, "frame buffer init \n");
    frame_buffer_init();
    color_printk(WHITE, BLACK, "frame_buffer_init() is OK \n");

    color_printk(RED, BLACK, "pagetable init \n");
    pagetable_init();

    color_printk(RED, BLACK, "interrupt init \n");
    init_interrupt();

    // color_printk(RED, BLACK, "task_init \n");
    // task_init();

    while (1)
        ;
}
